# V figure 60 days: Intensity - share of death in X days after statr admission: ------------------------------
  do_figure60_days_revision <- function(con = ,
                                  path ="path",
                         file_name  = "revision_clalit_all_model" ,
                         file_name_no_adm  = "revision_clalit_all_model_noadm" ,
                         intensity_wards = c("oncology","internal_medicine","geriatry","rehabilitation"),
                         period = "1 Year",
                         compare = T,
                         p = 0) {
  results <- get(load((paste0(path,file_name,".RData"))))
  
  auc_adm <- NULL
  if (compare ==T ) {
    results_no_adm <-  get(load(paste0(path,file_name_no_adm,".RData")))
    auc_adm<- rbind(results$roc_auc %>% mutate(features= "all"),
                    results_no_adm$roc_auc %>% mutate(features= "without EMR"))
  }
  
  
  res_df<- results$preds_test %>% 
    mutate(across(contains("category"),as.character)) %>% 
    mutate(across(contains("profession"),as.character)) %>% 
    do_intensity() 
  
  data <-  get_revision_cancer_monthly_data(con) %>%
    filter(S_sample_source_XX == "cnr_data") %>% 
    select(id_var,S_index_date_XX ,DMG_date_of_death_XX) %>%
    rename(S_original_index_date_XX = S_index_date_XX) %>% 
    collect()  
  
  rres_df_death <- res_df %>% 
    left_join(data, by = "id_var") %>% data.table()

  dt_hosp_cost_cancer_dyn_prob <- rres_df_death[,.(DMG_date_of_death_XX, 
                                                   S_original_index_date_XX,
                                                   min_event_date = as.Date(S_original_index_date_XX)+EVE_event_time,
                                                   new_profession =  EVE_intensity,
                                                   dyn_prob  =preds_after_bayes )]

  dt_hosp_cost_cancer_dyn_prob[,date_start_death := as.numeric(difftime(DMG_date_of_death_XX, min_event_date, units = "days"))]
  
  dt_hosp_cost_cancer_dyn_prob[, `:=`(died_7_days = (!is.na(date_start_death) & date_start_death >=0 & date_start_death <=7) ,
                                      died_14_days = (!is.na(date_start_death) & date_start_death >=0 & date_start_death <=14) ,
                                      died_30_days = (!is.na(date_start_death) & date_start_death >=0 & date_start_death <=30) ,
                                      died_60_days = (!is.na(date_start_death) & date_start_death >=0 & date_start_death <=60) ) 
                               ]
  
  
  dt_hosp_cost_cancer_dyn_prob[,Intensity := new_profession]
  
  death_adm_dyn_bins_Intensity <- dt_hosp_cost_cancer_dyn_prob[,
                                                               .("7 days"= mean(died_7_days),
                                                                 "14 days"= mean(died_14_days),
                                                                 "30 days"= mean(died_30_days),
                                                                 "60 days"= mean(died_60_days),
                                                                 N=as.double(.N)),
                                                               
                                                               by= .(Intensity , 
                                                                     bins = plyr::round_any(dyn_prob, 0.10, floor)) ][
                                                                       order(bins)]
  
  
  combine_death_adm_bins_Intensity <- rbind(
    death_adm_dyn_bins_Intensity[,type:="Near the Event"]
  )
  
  combine_death_adm_bins_long_Intensity <- 
    melt(combine_death_adm_bins_Intensity,
         id.vars= c("bins","type","Intensity") ,
         measure.vars = names(death_adm_dyn_bins_Intensity)[3:6])
  
  combine_death_adm_bins_long_Intensity[,variable:=factor(variable, levels = c("60 days", "30 days", "14 days" ,"7 days"))]
  
  write.csv(combine_death_adm_bins_long_Intensity[variable=="60 days" &
                                                    type == "Near the Event",],
            file=paste0("figure_60days_",gsub(" ","",period),".csv"))
  
  
  pdf(file=paste0("figure_60days_",gsub(" ","",period),".pdf"))
  print(ggplot()+
          geom_density(data = dt_hosp_cost_cancer_dyn_prob,
                       aes(x=dyn_prob, y=..scaled.., fill=Intensity),
                       size = 0.7,
                       color=NA,
                       alpha = 0.2) +
          geom_line(data =combine_death_adm_bins_long_Intensity[variable=="60 days" &
                                                                  type == "Near the Event",],
                    aes(x=(bins+0.05) ,y= value, linetype = Intensity))+
          scale_x_continuous(breaks = seq(0, 1, 0.10), limits = c(0,1)) +
          scale_y_continuous(breaks = seq(0, 1, 0.10), limits = c(0,1)) +
          scale_fill_manual( values = c("grey5","grey50"))+
          labs(y="",
               x = paste0("Current Predicted ", period, " Mortality Risk"),
               linetype="Intensity",
               fill ="Intensity")+
          ylim(0,1) +
          theme(legend.position = "none") +
          theme(aspect.ratio = 1)   +
          annotate(geom="text", x=0.7, y=0.07, label="Light Density: Low Intensity", size = 3)+
          annotate(geom="text", x=0.7, y=0.02, label="Dark Density: High Intensity", size = 3)+
          annotate(geom="text", x=0.58-p, y=0.35+p, label="Low Intensity", size = 5)+
          annotate(geom="text", x=0.75-p, y=0.17+p, label="High Intensity", size =5 )
  )
  dev.off()
  return(auc_adm)
  }
  